using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Xml;
using System.Drawing;
using System.Web.UI.WebControls;

namespace BlogControl
{
    class CommentsManager
    {
        private const string UserNameTextBox = "comment_name_";
        private const string SubjectTextBox = "comment_subject_";
        private const string MessageTextBox = "comment_body_";
        internal static void AddComment(Blog main, HtmlTextWriter output)
        {
            string id = main.CommandArgument;
            if (id == null) throw new ArgumentNullException("Could not find post id");
            string username = HttpContext.Current.Request.Params[UserNameTextBox + id];
            string subject = HttpContext.Current.Request.Params[SubjectTextBox + id];
            string body = HttpContext.Current.Request.Params[MessageTextBox + id];
            
            
            FilesManager.AddComment(main.Directory, id, username, subject, body, DateTime.UtcNow);
            CommentArguments ca = new CommentArguments(subject, username, body, id + ".xml", id);

            main.TriggerCommentAdded(ca);
        }

        internal static void WriteAddCommentPanel(HtmlTextWriter writer, string id, Blog control)
        {
            JavascriptManager.WriteCommentScripts(writer);
           
            writer.AddAttribute( HtmlTextWriterAttribute.Onclick, "javascript:showAddComment('comment_" + id + "');");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.AddCommentLinkColor));
            writer.AddStyleAttribute( HtmlTextWriterStyle.Cursor,"pointer");
            if (control.AddCommentLinkCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.AddCommentLinkCSS);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(control.AddCommentLinkText);
            writer.RenderEndTag();
            writer.WriteBreak();
           
            writer.AddAttribute( HtmlTextWriterAttribute.Id,"comment_" + id);
            writer.AddStyleAttribute( HtmlTextWriterStyle.Display,"none");
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            
            writer.Write(control.AddCommentUserNameText+ ":");
            writer.RenderEndTag();//td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            HtmlManager.RenderTextBox(writer, UserNameTextBox + id, UserNameTextBox + id);

          
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
          
            writer.Write(control.AddCommentSubjectText+":");
            writer.RenderEndTag();//td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            HtmlManager.RenderTextBox(writer, SubjectTextBox + id, SubjectTextBox + id);
          
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
           
            writer.Write(control.AddCommentMessageText+":");
            writer.RenderEndTag();//td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.Write("<textarea " + (control.AddCommentTextAreaCSS != null ? "class=\"" + control.AddCommentTextAreaCSS + "\"" : null) + " cols=\"" + control.AddCommentTextAreaColumns + "\" rows=\"" + control.AddCommentTextAreaRows + "\" id=\"" + MessageTextBox + id + "\" name=\"" + MessageTextBox + id + "\"></textarea>");
            writer.RenderEndTag();//td
            writer.RenderEndTag();//tr

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            
            JavascriptManager.WriteHiddenPostId(writer);
            JavascriptManager.WriteCurrentAction(writer);

            Button button = (Button)control.Controls[control.addCommentSubmitButtonIndex];
            if (!string.IsNullOrEmpty(control.AddCommentSubmitButtonCSS))
            {
                button.CssClass = control.AddCommentSubmitButtonCSS;
               
            }
            button.OnClientClick = "javascript:setCurrentAction('AddComment');setCurrentId('" + id + "')";
            button.Text = control.AddCommentSubmitText;
            button.RenderControl(writer);
          

            writer.Write("<input " + (control.AddCommentCancelButtonCSS != null ? "class=\"" + control.AddCommentCancelButtonCSS + "\"" : null) + " type=\"button\" onclick=\"document.getElementById('comment_" + id + "').style.display='none' ;\" value=\"" + control.AddCommentCancelText + "\"/>");

            writer.RenderEndTag();
            writer.RenderEndTag();//tr

            writer.RenderEndTag();
        }

      
        private static void WriteComment(HtmlTextWriter writer, string author, string subject, string date, string body,Blog control)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            if (control.CommentsViewSubjectCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.CommentsViewSubjectCSS);
            }
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.CommentsSubjectColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(HttpUtility.HtmlEncode(subject));
            writer.RenderEndTag();
            if (control.CommentsViewAuthorCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.CommentsViewAuthorCSS);
            }
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.CommentsAuthorColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.Write(HttpUtility.HtmlEncode(author));
            writer.RenderEndTag();
            if (control.CommentsViewPublishDateCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.CommentsViewPublishDateCSS);
            }
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.CommentsPublishDateColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write(date);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.CommentsBodyColor));
            writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.Write(HttpUtility.HtmlEncode(body));
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        internal static void WriteComments(HtmlTextWriter writer, XmlNode node,Blog control)
        {
            if (control.CommentsViewCSS != null)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, control.CommentsViewCSS);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            XmlNodeList comments = node.SelectNodes("comment");
            for (int i = 0; i < comments.Count; i++)
            {
                string date;
                try
                {
                    DateTime dt = DateTime.Parse(comments[i].Attributes["date"].Value);
                    date = dt.ToString(control.DateFormat);

                }
                catch
                {
                    date = comments[i].Attributes["date"].Value;
                }
                WriteComment(writer, comments[i].Attributes["writer"].Value, comments[i].SelectSingleNode("subject").InnerText.Trim(), date, comments[i].SelectSingleNode("body").InnerText.Trim(),control);
                if (control.ShowCommentsSeparator)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "3");
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    if (!string.IsNullOrEmpty(control.CommentsSeparatorCSS))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, control.CommentsSeparatorCSS);
                    }
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Color, ColorTranslator.ToHtml(control.CommentsSeparatorColor));
                    writer.RenderBeginTag(HtmlTextWriterTag.Hr);
                    writer.RenderEndTag();//hr
                    writer.RenderEndTag();//td
                    writer.RenderEndTag();//tr
                }
            }
            writer.RenderEndTag();
        }

        internal static int GetCommentsCount(XmlDocument doc)
        {
            XmlNode node= doc.DocumentElement.SelectSingleNode("Comments");
            if (node == null) return 0;
            return node.SelectNodes("comment").Count;
        }
    }
}
